package com.nwork.cateye.fragment;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import com.handmark.pulltorefresh.library.PullToRefreshListView;
import com.lidroid.xutils.BitmapUtils;
import com.lidroid.xutils.HttpUtils;
import com.lidroid.xutils.bitmap.PauseOnScrollListener;
import com.lidroid.xutils.exception.HttpException;
import com.lidroid.xutils.http.ResponseInfo;
import com.lidroid.xutils.http.callback.RequestCallBack;
import com.lidroid.xutils.http.client.HttpRequest;
import com.nwork.cateye.Constants;
import com.nwork.cateye.R;
import com.nwork.cateye.adapter.MovieZhengListViewAdapter;
import com.nwork.cateye.model.MovieModel;
import com.nwork.cateye.utils.CommonUtils;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.LinkedList;
import java.util.List;

/**
 * MoviewFragment
 */
public class MovieZhengFragment extends Fragment {

    private PullToRefreshListView listView;
    private List<ImageView> imageViewList;
    private MovieZhengListViewAdapter adapter;
    private List<MovieModel> movieModels;
    private List<String> imgUrls;
    private BitmapUtils bitmapUtils;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_movie_zheng, container, false);
        initView(view);
        addData();
        listView.setOnScrollListener(new PauseOnScrollListener(bitmapUtils, false, true));

        return view;
    }

    private void initView(View view) {
        movieModels = new LinkedList<>();
        listView = (PullToRefreshListView) view.findViewById(R.id.zheng_list_view);
        imageViewList = new LinkedList<>();
        imgUrls = new LinkedList<>();
        bitmapUtils = new BitmapUtils(getActivity());
        bitmapUtils.configDefaultLoadingImage(R.drawable.bg_default_cat_gray);
    }

    private void addData() {
        HttpUtils httpUtils = new HttpUtils();
        httpUtils.send(HttpRequest.HttpMethod.GET, Constants.MOVIE_ALL, new RequestCallBack<Object>() {
            @Override
            public void onLoading(long total, long current, boolean isUploading) {
                Log.d("!!", "下载中");
                super.onLoading(total, current, isUploading);
            }

            @Override
            public void onSuccess(ResponseInfo<Object> objectResponseInfo) {
                Log.d("!!", "下载成功");
                if (objectResponseInfo != null) {
                    MovieModel movieModel = new MovieModel();
                    String movieJson = objectResponseInfo.result.toString();
                    try {
                        JSONObject jsonObject = new JSONObject(movieJson);
                        movieModel.parseJson(jsonObject);
                        List<MovieModel> models = movieModel.getMovieModels();
                        models.add(0, new MovieModel());
                        //得到所有的图片的地址
                        adapter = new MovieZhengListViewAdapter(getActivity(), models, bitmapUtils);
                        listView.setAdapter(adapter);
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                }
            }

            @Override
            public void onFailure(HttpException e, String s) {
                Log.d("!!", "下载失败");
            }
        });
    }


    //http://p0.meituan.net/240.332/movie/c35fad6f9777b62a836d3b9ddfb8ae26479884.jpg.webp
//        http://p0.meituan.net/w.h/movie/    c35fad6f9777b62a836d3b9ddfb8ae26479884.jpg
    //http://p0.meituan.net/mmc/5beefa93ae86c0d7f145bd21d6df07ac92003.jpg
    //http://p0.meituan.net/mmc/922c1b28a6a9a448c9e00d9e5709d9c560780.jpg
    //http://p1.meituan.net/mmc/1cfabd1ac484bc82f3a93a5c6a26e38529843.jpg
    //http://p0.meituan.net/mmc/d89a3426f634a43c718de0cc6032051d83136.jpg
}
